<template>
  <div>
    <div id="time">
      <span></span><input type="text" @click="openByDrop($event)" v-model="date" placeholder="请选择时间" readonly>
      <transition name="fade">
        <div class="calendar-dropdown" :style="{'left':calendar1.left+'px','top':calendar1.top+'px'}" v-if="calendar1.show" v-clickoutside="handleClose">
          <calendar :zero="calendar1.zero" :lunar="calendar1.lunar" :value="calendar1.value" :begin="calendar1.begin" :end="calendar1.end" @select="calendar1.select"></calendar>
        </div>
      </transition>
      <button  type="button" class="close" @click="date = ''">
        &times;
      </button>
    </div>
  </div>
</template>
<script>
import calendar from '@/components/calendar/calendar.vue'
import Bus from "@/components/common/bus"; //引入公共媒介JS BUS
export default {
     components: {
        calendar
    },
  
     data(){
        return {
             calendar1:{
                show:false,
                zero:true,
                //  value:[2018,2,16], //默认日期
                lunar:true, //显示农历
                select:(value)=>{
                    this.calendar1.show=false;
                    this.calendar1.value=value;
                    this.date=value.join("/");
                }
            },
            date:''
        }
    },
    watch:{
        date: {
          handler(curVal, oldVal) { //监听登陆人的科室有没改变，如果改变就执行下面的方法
          //  console.log(curVal)
          //  console.log(oldVal)
          if(curVal != oldVal){
           Bus.$emit("timePicker", curVal); 
          }
             
            },
        }
    },
     methods:{
        openByDrop(e){
            this.calendar1.show=true;
            this.calendar1.left=e.target.offsetLeft-11;
            this.calendar1.top=e.target.offsetTop+40;
             e.stopPropagation(); //阻止时间冒泡到父元素
        },
       handleClose(){
          this.calendar1.show=false;
        }

}
}
</script>
<style scoped>

  #time {
    position: relative;
    width: 190px;
    height: 34px;
    border: 1px solid #CCC;
  }

    #time>span {
      display: inline-block;
      width: 39px;
      height: 34px;
      border-right: 1px solid #ccc;
      float: left;
    }

      #time>span:before {
        display: inline-block;
        content: "";
        width: 17px;
        height: 17px;
        background-image: url('../../../static/images/menology.png');
        margin-left: 10px;
        margin-top: 6px;
      }
    button.close{
    position: absolute;
    top: -2px;
    right: 0;
    outline: none;
    z-index: 2;
    width: 34px;
    height: 34px;
    text-align: center;

    }
    #time input {
      width: 120px;
      height: 32px;
      border: none;
      float: left;
      padding-left: 5px;
      font-size:14px;
    }

  .fade-enter-active,
  .fade-leave-active {
    transition: all .5s ease-in-out;
  }

  .fade-enter, .fade-leave-active {
    opacity: 0;
    transform: translateY(-10px);
  }
  /*下拉框*/
  .calendar-dropdown {
    background: #fff;
    position: absolute;
    left: 0;
    top: 0;
    padding: 5px;
    border: 1px solid #eee;
    border-radius: 2px;
    z-index: 999;
    box-shadow: 0px 2px 10px #ccc;
  }

    .calendar-dropdown:before {
      position: absolute;
      left: 30px;
      top: -10px;
      content: "";
      border: 5px solid rgba(0, 0, 0, 0);
      border-bottom-color: #DEDEDE;
    }

    .calendar-dropdown:after {
      position: absolute;
      left: 30px;
      top: -9px;
      content: "";
      border: 5px solid rgba(0, 0, 0, 0);
      border-bottom-color: #fff;
    }

  /*弹出框*/
  .calendar-dialog {
    position: absolute;
    left: 0;
    top: 0;
    right: 0;
    bottom: 0;
  }

  .calendar-dialog-mask {
    background: rgba(255,255,255,.5);
    width: 100%;
    height: 100%;
  }

  .calendar-dialog-body {
    background: #fff;
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%,-50%);
    padding: 10px;
    border: 1px solid #eee;
    border-radius: 2px;
  }
</style>
